//
//  LoadViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/24.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "LoadViewController.h"

@interface LoadViewController ()

@end

@implementation LoadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self begin];
    // Do any additional setup after loading the view.
}

- (void)begin{
    //创建 文件夹
    NSString *path = [[NSBundle mainBundle] pathForResource:@"topic" ofType:@"json"];
    NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path];
    NSError *error;
    NSDictionary* jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    
    
    
    NSString *cachepath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
       //创建一个 目录
    NSLog(@"沙盒:%@",NSHomeDirectory());
    NSString * realFatherPath = [NSString stringWithFormat:@"%@/%@",cachepath,@"mine_image"];
       ({
           BOOL ducisDir = YES;
           // fileExistsAtPath 判断一个文件或目录是否有效，isDirectory判断是否一个目录
           NSFileManager* fileManager = [NSFileManager defaultManager];
           BOOL existed = [fileManager fileExistsAtPath:realFatherPath isDirectory:&ducisDir];
           if(!existed){
               [fileManager createDirectoryAtPath:realFatherPath withIntermediateDirectories:YES attributes:nil error:nil];
               DDLogInfo(@"创建目录成功:%@",realFatherPath);
           }else{
               static dispatch_once_t onceToken;
               dispatch_once(&onceToken, ^{
                   DDLogInfo(@"该目录已经存在:%@",realFatherPath);
               });
               
           }
       });
    NSArray * array = jsonObj[@"data"];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    for (NSDictionary * dic in array) {
        NSString * name = [dic[@"title"] description];
        NSString * realFatherPath = [NSString stringWithFormat:@"%@/%@/%@",cachepath,@"mine_image",name];
        [fileManager createDirectoryAtPath:realFatherPath withIntermediateDirectories:YES attributes:nil error:nil];
        DDLogInfo(@"创建目录成功:%@",realFatherPath);
        
        ({
            NSString * url = [dic[@"icon"] description];
            
            [SDWebImageManager.sharedManager loadImageWithURL:url.mj_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                
            } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
                
                if (image) {
                    NSString * imageFatherPath = [NSString stringWithFormat:@"%@/%@/%@/%@",cachepath,@"mine_image",name,@"icon.png"];
                    [UIImagePNGRepresentation(image) writeToFile:imageFatherPath atomically:1];
                }
            }];
        });

//        ({
//            NSString * url = [dic[@"speedy_bg"][@"background"] description];
//
//            [SDWebImageManager.sharedManager loadImageWithURL:url.mj_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
//
//            } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
//
//                if (image) {
//                    NSString * imageFatherPath = [NSString stringWithFormat:@"%@/%@/%@/%@",cachepath,@"mine_image",name,@"background.png"];
//                    [UIImagePNGRepresentation(image) writeToFile:imageFatherPath atomically:1];
//                }
//            }];
//        });
//
//        ({
//            NSString * url = [dic[@"speedy_bg"][@"big_img"] description];
//
//            [SDWebImageManager.sharedManager loadImageWithURL:url.mj_url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
//
//            } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
//
//                if (image) {
//                    NSString * imageFatherPath = [NSString stringWithFormat:@"%@/%@/%@/%@",cachepath,@"mine_image",name,@"big_img.png"];
//                    [UIImagePNGRepresentation(image) writeToFile:imageFatherPath atomically:1];
//                }
//            }];
//        });
//        NSString * jsonFatherPath = [NSString stringWithFormat:@"%@/%@/%@/%@",cachepath,@"mine_image",name,@"json"];
//        [dic writeToFile:jsonFatherPath atomically:1];
    }
    
}

@end
